using System.Collections.Generic;

namespace Game.Plugin.Parse
{
    public class MinusOperator : IExpression 
    {
        readonly ICodeObject left;
        readonly ICodeObject right;

        public MinusOperator(
            ICodeObject left,
            ICodeObject right) 
        { 
            this.left = left; 
            this.right = right;
        }

        public object Interpret(Dictionary<string, ICodeObject> variables)  
        {
            object x = left.Interpret(variables);
            object y = right.Interpret(variables);
            if (x is int && y is int)
            {
                return (int)x - (int)y;
            }

            return new ErrorObject();
        }
    }

    public class MinusParser : IParser
    {
        public bool CanParse(string data)
        {
            return (data == "-");
        }

        public ICodeObject Parse(
            string token,
            ParserContext context)
        {
            IExpression e = new MinusOperator(
                context.Stack.Pop(),
                context.Stack.Pop());
            context.Stack.Push(e);
            return e;
        }
    }
}